import { Entity, PrimaryGeneratedColumn, Column, Index } from "typeorm";
import { ManualTimestamp } from "../../../decorators/manual-timestamp.decorator";

export enum OperationType {
  // 大小玩法操作
  DICE_BET = 'dice_bet',           // 大小下注
  DICE_DRAW = 'dice_draw',         // 大小开奖
  DICE_SETTLE = 'dice_settle',     // 大小结算

  // 赤壁玩法操作
  CHIBI_BET = 'chibi_bet',         // 赤壁下注
  CHIBI_DRAW = 'chibi_draw',       // 赤壁开奖
  CHIBI_SETTLE = 'chibi_settle',   // 赤壁结算

  // 猜拳玩法操作
  RPS_CHALLENGE = 'rps_challenge', // 发起猜拳
  RPS_RESPOND = 'rps_respond',     // 应战猜拳
  RPS_SETTLE = 'rps_settle',       // 猜拳结算

  // 系统操作
  SYSTEM_CONFIG = 'system_config', // 系统配置
  SYSTEM_MAINTENANCE = 'system_maintenance', // 系统维护
  USER_LOGIN = 'user_login',       // 用户登录
  USER_LOGOUT = 'user_logout',     // 用户登出
}

@Entity('casino_operation_logs')
@ManualTimestamp()
@Index(['userId', 'operationType'])
@Index(['operationType', 'created_at'])
@Index(['created_at'])
export class CasinoOperationLog {
  @PrimaryGeneratedColumn()
  id!: number;

  @Column({ comment: '用户ID' })
  userId!: number;

  @Column({ type: 'varchar', length: 32, comment: '操作类型' })
  operationType!: OperationType;

  @Column({ type: 'text', comment: '操作数据', nullable: true })
  operationData?: any;

  @Column({ type: 'varchar', length: 45, comment: 'IP地址', nullable: true })
  ipAddress?: string;

  @Column({ type: 'text', comment: '用户代理', nullable: true })
  userAgent?: string;

  @Column("datetime", { name: "created_at", comment: "创建时间" })
  created_at!: Date;
// 计算属性
  get operationTypeName(): string {
    const names = {
      [OperationType.DICE_BET]: '大小下注',
      [OperationType.DICE_DRAW]: '大小开奖',
      [OperationType.DICE_SETTLE]: '大小结算',
      [OperationType.CHIBI_BET]: '赤壁下注',
      [OperationType.CHIBI_DRAW]: '赤壁开奖',
      [OperationType.CHIBI_SETTLE]: '赤壁结算',
      [OperationType.RPS_CHALLENGE]: '发起猜拳',
      [OperationType.RPS_RESPOND]: '应战猜拳',
      [OperationType.RPS_SETTLE]: '猜拳结算',
      [OperationType.SYSTEM_CONFIG]: '系统配置',
      [OperationType.SYSTEM_MAINTENANCE]: '系统维护',
      [OperationType.USER_LOGIN]: '用户登录',
      [OperationType.USER_LOGOUT]: '用户登出',
    };
    return names[this.operationType] || '未知操作';
  }

  get isGameOperation(): boolean {
    return [
      OperationType.DICE_BET,
      OperationType.DICE_DRAW,
      OperationType.DICE_SETTLE,
      OperationType.CHIBI_BET,
      OperationType.CHIBI_DRAW,
      OperationType.CHIBI_SETTLE,
      OperationType.RPS_CHALLENGE,
      OperationType.RPS_RESPOND,
      OperationType.RPS_SETTLE,
    ].includes(this.operationType);
  }

  get isSystemOperation(): boolean {
    return [
      OperationType.SYSTEM_CONFIG,
      OperationType.SYSTEM_MAINTENANCE,
    ].includes(this.operationType);
  }

  get isUserOperation(): boolean {
    return [
      OperationType.USER_LOGIN,
      OperationType.USER_LOGOUT,
    ].includes(this.operationType);
  }

  // 设置操作数据
  setOperationData(data: any): void {
    this.operationData = data;
  }

  // 获取操作数据
  getOperationData<T = any>(): T | undefined {
    return this.operationData as T;
  }

  // 设置IP地址
  setIpAddress(ip: string): void {
    this.ipAddress = ip;
  }

  // 设置用户代理
  setUserAgent(agent: string): void {
    this.userAgent = agent;
  }

  // 格式化创建时间
  get formattedCreatedAt(): string {
    return this.created_at.toLocaleString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit',
    });
  }
} 